package cn.rubintry.bug.hunting.view

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.media.MediaPlayer
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import android.widget.FrameLayout
import androidx.activity.ComponentActivity
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import cn.rubintry.animate.AnimateUtils
import cn.rubintry.animate.core.listener.OnAnimationListener
import cn.rubintry.animate.core.param.AnimatorParam
import cn.rubintry.animate.core.param.AnimatorType
import cn.rubintry.animate.core.param.Value
import cn.rubintry.bug.hunting.R
import cn.rubintry.bug.hunting.activity.MainActivity
import cn.rubintry.bug.hunting.common.extension.visible
import cn.rubintry.bug.hunting.databinding.SplashViewBinding
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class SplashView : FrameLayout {
    private var animationJob: Job?= null
    private var binding: SplashViewBinding ?= null
    private var mp = MediaPlayer()

    constructor(context: Context) : super(context){
        initView()
    }
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs){
        initView()
    }
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ){
        initView()
    }

    fun initView(){
        val view = View.inflate(context , R.layout.splash_view , null)
        binding = SplashViewBinding.bind(view)
        binding?.root?.isClickable = true
        addView(binding?.root)
        mp = MediaPlayer.create(context , R.raw.chadi)
        (context as? ComponentActivity)?.lifecycle?.addObserver(object : LifecycleEventObserver{
            override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
                if(event == Lifecycle.Event.ON_DESTROY){
                    animationJob?.cancel()
                    mp.release()
                }
            }

        })
    }


    /**
     * 开始动画
     */
    fun startAnimation(callback: () -> Unit){
        animationJob = CoroutineScope(Dispatchers.Main).launch {
            delay(700)
            AnimateUtils.with(binding?.imgKuWu)
                .translation(ScreenUtils.getScreenWidth() / 3 * 2f, ScreenUtils.getScreenHeight() / 2f, true)
                .setDuration(200)
                .async()
                .start()
            delay(200)
            mp.start()
            delay(2000)
            mp = MediaPlayer.create(context , R.raw.hi_lai_shin)

            AnimateUtils.with(binding?.imgKeqing)
                .translation(0f, ScreenUtils.getScreenWidth() / 2f, false)
                .setDuration(200)
                .async()
                .addListener(object : OnAnimationListener{
                    override fun onEnd() {
                        AnimateUtils.with(binding?.imgKuWu)
                            .alpha(1f , 0f , false)
                            .setDuration(200)
                            .async()
                            .start()
                    }

                })
                .start()
            AnimateUtils.with(binding?.imgKeqing)
                .alpha(0.3f , 1f , false)
                .setDuration(1000)
                .async()
                .start()
            mp.start()

            delay(1000)

            AnimateUtils.with(binding?.imgKeqing)
                .scale(1f , 2f , false)
                .setDuration(1000)
                .async()
                .start()

            AnimateUtils.with(binding?.root)
                .alpha(1f , 0f , false)
                .setDuration(1000)
                .async()
                .addListener(object : OnAnimationListener{
                    override fun onEnd() {
                        removeView(binding?.root)
                        callback.invoke()
                    }

                })
                .start()
        }
    }


}